<?php
/**
 * Created by PhpStorm.
 * User: wanglin
 * Date: 2018/11/15
 * Time: 10:30
 */

namespace App\Console\Commands\ThirdOrder;


use App\Console\Base;
use App\Services\PartanerTips;
use App\Services\ThirdAPI\BGAPIService;
use common\utils\RedisKeys;

class BGTipsProcessor extends Base
{
    /**
     * 控制台命令 signature 的名称。
     *
     * @var string
     */
    protected $signature = self::PREFIX . 'getBgTips';

    /**
     * 控制台命令说明。
     *
     * @var string
     */
    protected $description = '获取bg小费';

    // 拉取注单的区间长度，比如从 4:00 - 5:00 一个小时的长度，根据注单量调整
    public $timeInterval = 60 * 60;
    // 自动重拉更新锁 redis key
    public $lockRedisKey = RedisKeys::BG_TIPS_LOCK_AUTOFIX;
    // 拉取注单更新时间 redis key
    public $updateRedisKey = RedisKeys::BG_TIPS_UPDATE_TIME;
    // 当前最新的拉取注单更新时间
    public $latestUpdateKey;

    public function handle()
    {
        $data = $this->getTipDatas();
         PartanerTips::handleData($data);
         $this->info($this->description.'采集完成'.date("Y-m-d H:i:s"));
    }


    public function getServices()
    {
        return new BGAPIService();
    }

    public function getTipDatas()
    {
        $service = $this->getServices();
        // 检测是否需要重新拉取注单，每次重拉 1 小时前的注单
        $startTime = OrderStrategy::getUpdateKey($this->lockRedisKey, $this->updateRedisKey);
        $startTime = strtotime($startTime);
        // 拉取 $this->timeInterval 的数据，减3秒是防止出现临界数据
        $this->latestUpdateKey = $endTime = min(time(), $startTime + $this->timeInterval - 20*60);
        $data = $service->getTips($startTime, $endTime);
        return $data;
    }


}